<?php
/**
 * User: cooper
 * Date: 8/31/13
 * Time: 9:16 PM
 */

class GalleryController extends AppController
{
    
    // model name
    public $name = 'Gallery';
    
    // include component
    public $components = array('File');
    
    // uses another model
    public $uses = array('Category', 'Gallery');
    
    /**
     *  INDEX FE
     */
    public function index()
    {
        $this->title_for_layout = 'Галерея';
        
        $this->Gallery->recursive = -1;
        $list = $this->Gallery->find('all');
        $this->set('list', $list); 
    }

    
    public function category($id)
    {
        $this->title_for_layout = 'Галерея';
        
        
        if(!$id)
        {
            // get images by id category
            $this->Gallery->recursive = 0;
            $list = $this->Gallery->find('all');
            // debug($list); die;

            $this->set('list', $list);

        } else {
            
            // get images by id category
            $this->Gallery->recursive = 0;
            $list = $this->Gallery->find('all' , array('conditions' => array('Gallery.category_id' => $id))) ;
            // debug($list); die;

            $this->set('list', $list);
        
        }
        
    }
    
    
    /**
     *  ALL IMAGES DISPLAY
     */
    public function admin_index()
    {
        // get all galery images
        if($this->request->is('post') && $this->request->data)
        {
            // @todo: select by id
        } else {
        
            $this->Gallery->recursive = -1;
            $list = $this->Gallery->find('all');
            $this->set('list', $list);    
        }
        
    }
        
    /**
     *  upload new images and seve file in the database
     * 
     *  
     */
    public function admin_add()
    {
        // default behavior, select
        
        $cat = $this->Category->find('all');
        $category = array();
        foreach ($cat as $val) 
        {
            $category[] = array(
                'value' => $val['Category']['id'], 
                'name' => $val['Category']['title']
                    ) ;
        
        }
        // set template value $category - array category
        $this->set('category', $category);
        
        
        // if post
        if($this->request->is('post') && $this->request->data)
        {
            $request = $this->request->data; 
            // validation, check and move file
            $result = $this->File->upload_process($request, Configure::read('gallery_img_upload_dir'));
            
            // save data
            if(isset($result) && (strlen($result) > 0))
            {
                // trunk WWW ROOT path
                $request['Gallery']['img_path'] = str_replace(WWW_ROOT, DS, $result);
                // debug($request); die;
                
                if($this->Gallery->save($request))
                {
                    $this->Session->setFlash('Ready');
                    $this->redirect('index');
                } else {
                    // @todo: write in log
                    $this->Session->setFlash('Error');
                    $this->redirect('index');
                }
            } else {
                //$this->Session->setFlash('Error1');
                $this->redirect('index');
            }
        }
    }
    
    /**
     *  upadte category
     * @param type $data
     */
    public function admin_edit($id)
    {
        if ( !is_numeric($id) ) return false;
        
        if($this->request->is('post') && $this->request->data)
        {
            $request = $this->request->data;
            
            // isset file    
            if(is_array($request['Gallery']['img_path']) && (count($request['Gallery']['img_path'] == 5)))
            {
                $result = $this->File->upload_process($request, Configure::read('gallery_img_upload_dir'));
            }  
             
            // save data
            if(isset($result) && (strlen($result) > 0))
            {
                $request['Gallery']['img_path'] = str_replace(WWW_ROOT, DS, $result);
                // debug($request); die;
            } else {
                // seve old file
                
                $this->Gallery->recursive = -1;
                $res = $this->Gallery->read(null, $id);
                $request['Gallery']['img_path'] = $res['Gallery']['img_path'];
            }
            
            $this->Gallery->id = $id;
             
            if($this->Gallery->save($request))
            {
                $this->Session->setFlash('Ready');
                $this->redirect('index');
            } else {
                // @todo: write in log
                $this->Session->setFlash('Error');
                $this->redirect('index');
            }
            
        }
        
        
        // show edit data
       
        $this->Gallery->recursive = -1;
        $item = $this->Gallery->read(null, $id);
        if($item)
        {
            $this->set('item', $item);
        } else {
            $this->Session->setFlash('Error retrive edit data');
            $this->redirect('index');
        }
    }
    
    
    /**
     *  delete category
     */
    public function admin_delete($id)
    {
        
        if(!$id && !is_numeric($id))
        {
            $this->Session->setFlash('wrong request');
            $this->redirect('index');
        } 
        
        // read link file for delete
        $this->Gallery->recursive = -1;
        $file_unlink = $this->Gallery->read(null, $id);
        
        // debug($file_unlink); die;
        // @todo: WWW_ROOT for path
        $res = $this->File->delete_file($file_unlink['Gallery']['img_path']);
        
        if($res)
        {
            $this->Gallery->delete($id);
            $this->Session->setFlash('delete success');
            $this->redirect('index');
        } else {
            $this->Session->setFlash('delete error');
            $this->redirect('index');
        }
    }
}